package com.scu.hotelsystem.controller;

import com.scu.hotelsystem.pojo.BaseResult;
import com.scu.hotelsystem.pojo.Guest;
import com.scu.hotelsystem.pojo.GuestSearch;
import com.scu.hotelsystem.pojo.PageInfo;
import com.scu.hotelsystem.service.GuestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller

public class GuestController {
    @Autowired
    private GuestService guestService;
/*
    @RequestMapping("getGuestList")
    public ModelAndView getGuestList(){

        ModelAndView view = new ModelAndView("guestlist");
        List<Guest> guestList =guestService.getGuestList();
        view.addObject("guestList",guestList);
        return view;

    }
*/

    @RequestMapping("getGuestListByPage")
    @ResponseBody
    public Map getGuestListByPage(GuestSearch info){

        List<Guest> guestList = guestService.getGuestListByPage(info);
        int count = guestService.getGuestCount(info);

        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count); //总条数
        map.put("data",guestList); //当前页显示的数据

        return map;

    }

    @RequestMapping("getGuestMain")
    public ModelAndView getGuestMain(){
        ModelAndView view = new ModelAndView("guestlist");
        return view;

    }
    //删除房客信息
    @RequestMapping("delGuest")
    @ResponseBody
    public BaseResult delGuest(Integer id){
        int res = guestService.delGuest(id);
        BaseResult result = new BaseResult();
        if (res > 0){
            result.setCode(0);
        }
        else{
            result.setCode(-1);
            result.setMsg("删除失败！");
        }
        return result;
    }

    @RequestMapping("addGuest")
    @ResponseBody
    public BaseResult addGuest(Guest guest){
        int resp = guestService.addGuest(guest);
        BaseResult result = new BaseResult();
        if (resp > 0){
            result.setCode(0);
        }
        else{
            result.setCode(-1);
            result.setMsg("添加失败！");
        }
        return result;

    }
}
